<?php

/**
 * SimplifyPHP Framework
 *
 * This file is part of SimplifyPHP Framework.
 *
 * SimplifyPHP Framework is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * SimplifyPHP Framework is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * @author Rodrigo Rutkoski Rodrigues, <rutkoski@gmail.com>
 */

/**
 *
 * Default implementation of IDataHolder
 *
 * @author "Rodrigo Rutkoski Rodrigues, <rutkoski@gmail.com>"
 *
 */
class DataHolder extends Dictionary implements IDataHolder
{

  /**
   * 
   * @var DataFilter
   */
  public $filters;

  /**
   * Holds modified data
   * 
   * @var array
   */
  protected $modified = array();

  /**
   * Constructor.
   * 
   * @param array $data initial data
   * @return void
   */
  public function __construct($data = null)
  {
    $this->filters = new DataFilter();
    $this->reset($data);
  }

  /**
   * (non-PHPdoc)
   * @see simplify/kernel/data/SimpleDictionary#_del($name)
   */
  protected function _del($name)
  {
    if (isset($this->modified[$name])) {
      unset($this->modified[$name]);
    }
    
    return parent::_del($name);
  }

  /**
   * (non-PHPdoc)
   * @see simplify/kernel/data/Dictionary#_get($name)
   */
  protected function _get($name)
  {
    if (isset($this->modified[$name])) {
      return $this->modified[$name];
    }
    
    return parent::_get($name);
  }

  /**
   * (non-PHPdoc)
   * @see simplify/kernel/data/Dictionary#_has($name)
   */
  protected function _has($name)
  {
    return parent::_has($name) || isset($this->modified[$name]);
  }

  /**
   * (non-PHPdoc)
   * @see simplify/kernel/data/Dictionary#_set($name, $value)
   */
  protected function _set($name, $value)
  {
    if (parent::get($name) === $value) {
      unset($this->modified[$name]);
    }
    else {
      $this->modified[$name] = $value;
    }
    
    return $this;
  }

  /**
   * Apply data filters to $name or to all names in the data holder.
   * 
   * @param string $name value name
   * @return DataHolder
   */
  public function applyFilters($name = null)
  {
    $this->filters->applyFilters($this->data, $name);
    $this->filters->applyFilters($this->modified, $name);
    return $this;
  }

  /**
   * (non-PHPdoc)
   * @see simplify/kernel/data/api/IDataHolder#commit()
   */
  public function commit()
  {
    $this->data = array_merge($this->data, $this->modified);
    $this->modified = array();
    return $this;
  }

  /**
   * (non-PHPdoc)
   * @see simplify/kernel/data/Dictionary#getAll()
   */
  public function getAll()
  {
    return array_merge(parent::getAll(), $this->modified);
  }

  /**
   * (non-PHPdoc)
   * @see simplify/kernel/data/api/IDataHolder#getModified()
   */
  public function getModified()
  {
    return $this->modified;
  }

  /**
   * (non-PHPdoc)
   * @see simplify/kernel/data/SimpleDictionary#getNames()
   */
  public function getNames()
  {
    return array_merge(parent::getNames(), array_keys($this->modified));
  }

  /**
   * (non-PHPdoc)
   * @see simplify/kernel/data/SimpleDictionary#reset($data)
   */
  public function reset($data = null)
  {
    $this->modified = array();
    return parent::reset($data);
  }

  /**
   * Set a data filter.
   * 
   * @param string $name value name 
   * @param IFilter $filter data filter
   * @return DataHolder
   */
  public function setFilter($name, IFilter $filter)
  {
    $this->filters->setFilter($name, $filter);
    return $this;
  }

}

?>